package com.fang.gittest;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 封装了对命令的调用实现
 * 主要的作用是调用git命令
 */
public class CommandCallAndParse {


    /**
     * 执行命令
     *
     * @param command
     * @return
     */
    public String command(String command) {
        StringBuilder changes = new StringBuilder();
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedInputStream bis = new BufferedInputStream(
                    process.getInputStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(bis));

            String line;
            while ((line = br.readLine()) != null) {
                changes.append(line).append("\n");
            }
            bis.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return changes.toString();
    }
    
    public String callGitChange() {
        String command = " git diff --stat ";
        return command(command);
    }

    public String callGitDiff(String path) {
        String command = "git diff " + path;
        return command(command);
    }


}
